package com.zhiyou.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.zhiyou.app.common.exception.BizException;
import com.zhiyou.app.pojo.entity.ComWalletEntity;
import com.zhiyou.app.mapper.ComWalletMapper;
import com.zhiyou.app.service.IComWalletService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author jonathan.z
 * @since 2025-07-03
 */
@Slf4j
@Service
public class ComWalletServiceImpl extends ServiceImpl<ComWalletMapper, ComWalletEntity> implements IComWalletService {
    @Value("${wallet.com-id:1}")
    private String comWalletId;

    public void addComWalletAmount(BigDecimal amount) {
        log.info("添加企业钱包金额：{}分", amount);
        if (amount == null) {
            throw new BizException("添加企业钱包金额不能为空");
        }
        if (amount.intValue() == 0) {
            return;
        }
        // 构建 LambdaUpdateWrapper
        LambdaUpdateWrapper<ComWalletEntity> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper
                // 根据钱包ID筛选
                .eq(ComWalletEntity::getId, Integer.valueOf(comWalletId))
                // SQL: balance = balance + amount
                .setSql("total_amount = total_amount " + (amount.intValue() > 0 ? "+" : "") + amount);
        // 执行更新（会自动生成 UPDATE 语句）
        update(updateWrapper);
    }
}
